package com.orhon.smartcampus.modules.baseinfo.mutation;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.baseinfo.dao.MessageCategoryDao;
import com.orhon.smartcampus.modules.baseinfo.dao.TimetableDao;
import com.orhon.smartcampus.modules.baseinfo.entity.MessageCategory;
import com.orhon.smartcampus.modules.baseinfo.entity.Timetable;
import com.orhon.smartcampus.modules.baseinfo.gqlInputModel.GQLMessageCategoryInput;
import com.orhon.smartcampus.modules.baseinfo.gqlInputModel.GQLTimetableInput;
import com.orhon.smartcampus.modules.baseinfo.inputMapper.MessageCategoryInputMapper;
import com.orhon.smartcampus.modules.baseinfo.inputMapper.TimetableInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class AddMessageCategoryMutation implements DataFetcher {


    @Autowired
    MessageCategoryDao messageCategoryDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 添加消息分类
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        Long userId = JwtUtils.getUserId(token);

        GQLMessageCategoryInput input = MessageCategoryInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        MessageCategory messageCategory = null;

        if (input.getId() != null) {
            messageCategory = messageCategoryDao.get(MessageCategory.class, input.getId());
        } else {
            messageCategory = new MessageCategory();
            messageCategory.setCreateTime(new Date());
            messageCategory.setCreateUser(userId);
        }

        if(input.getCategoryName() != null){

            messageCategory.setCategoryName(input.getCategoryName());
        }
        if (schoolId != null) {
            messageCategory.setBaseSchoolsBySchoolId(schoolsDao.get(Schools.class, schoolId));
        }
        if(input.getCategoryValue() != null){

            messageCategory.setCategoryValue(input.getCategoryValue());
        }
        if(input.getCategoryExplain() != null){

            messageCategory.setCategoryExplain(input.getCategoryExplain());
        }
        if(input.getCreateUser() != null){

            messageCategory.setCreateUser(input.getCreateUser());
        }
        if(input.getTemplateId() != null){

            messageCategory.setTemplateId(input.getTemplateId());
        }
        try {
            messageCategoryDao.saveOrUpdate(messageCategory);
        } catch (Exception e) {
            return Result.error(500, "" + e);
        }
        return Result.ok();
    }
}

